package linkedlist;

/**
 * @author 十八岁讨厌编程
 * @date 2025/6/27 21:11
 * @PROJECT_NAME leetcode
 * @description
 */

public class Solution24 {

    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode virtualHead = new ListNode(-1, head);
        ListNode exchangePointer = virtualHead;
        while (exchangePointer != null && exchangePointer.next != null && exchangePointer.next.next != null) {
            ListNode temp1 = exchangePointer.next;
            ListNode temp3 = temp1.next.next;
            exchangePointer.next = exchangePointer.next.next;
            exchangePointer.next.next = temp1;
            exchangePointer.next.next.next = temp3;
            exchangePointer = exchangePointer.next.next;
        }
        return virtualHead.next;
    }


}
